package com.centaline.androidsalesblog.fragments.calculate;

import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.BaseTaxCalFragment;
import com.centaline.androidsalesblog.model.businissobject.TaxCalculateBo;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.trello.rxlifecycle.FragmentEvent;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;

/**
 * 新房税费计算器
 * Created by ruanzb on 2016/3/3.
 */
public class NewHouseCalcFragment extends BaseTaxCalFragment {

    private AppCompatEditText aet_house_area, aet_house_unit_price;
    private Button btn_tax_fragment;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_calc_house_new;
    }

    @Override
    protected void findViews(View view) {
        aet_house_area = (AppCompatEditText) view.findViewById(R.id.aet_house_area);
        aet_house_unit_price = (AppCompatEditText) view.findViewById(R.id.aet_house_unit_price);
        btn_tax_fragment = (Button) view.findViewById(R.id.btn_tax_fragment);
    }

    @Override
    protected void initView() {

        Observable<CharSequence> houseAreaObservable = RxTextView.textChanges(aet_house_area);//房屋面积
        Observable<CharSequence> houseUnitPriceObservable = RxTextView.textChanges(aet_house_unit_price); //房屋单价

        //计算按钮
        Observable.combineLatest(houseAreaObservable, houseUnitPriceObservable,
                new Func2<CharSequence, CharSequence, Boolean>() {
                    @Override
                    public Boolean call(CharSequence charSequence, CharSequence charSequence2) {

                        boolean enable1 = !TextUtils.isEmpty(charSequence);
                        boolean enable2 = !TextUtils.isEmpty(charSequence2);
                        return enable1 && enable2;
                    }
                }).compose(this.<Boolean>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        btn_tax_fragment.setEnabled(aBoolean);
                    }
                });

        setCalcClick(btn_tax_fragment);
        super.initView();
    }

    @Override
    protected void initData() {

    }

    /**
     * 开始计算
     */
    @Override
    protected void startCalculate() {
        TaxCalculateBo taxCalculateBo = new TaxCalculateBo();
        taxCalculateBo.setModeType(2);
        taxCalculateBo.setHouseArea(Double.parseDouble(aet_house_area.getText().toString()));
        taxCalculateBo.setHouseUnitPrice(Double.parseDouble(aet_house_unit_price.getText().toString()));
        taxCalculateCallback.callBack(taxCalculateBo);
    }

}
